package com.incar.chuxing.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.incar.chuxing.R;
import com.incar.chuxing.baiduMap.GeoCoderUtils;
import com.incar.chuxing.bean.ParkDetailBean;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Michael Pi on 2016/9/5.
 */
public class ParkDetailPagerAdapter extends PagerAdapter {

    private TextView tvVpCarBrand;
    private TextView tv_vp_car_battery;
    private TextView tv_vp_car_seats;
    private TextView tv_vp_price;
    private TextView tv_vp_outtime;
    private TextView tv_vp_park_name;
    private TextView tv_vp_start_price;
    private TextView tv_vp_carr_distance;
    private ImageView iv_vp_pic;
    private TextView tv_vp_car_license;
    private Context context;
    private List<ParkDetailBean.AttachmentBean.CarsBean> list = null;
    private LayoutInflater inflater = null;

    public ParkDetailPagerAdapter(Context context, List<ParkDetailBean.AttachmentBean.CarsBean> list) {
        this.context = context;
        if (this.list != null) {
            this.list.clear();
        }
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = inflater.inflate(R.layout.item_detail_view_pager, container, false);
        tvVpCarBrand = (TextView) view.findViewById(R.id.tv_vp_car_brand);
        tv_vp_carr_distance = (TextView) view.findViewById(R.id.tv_vp_carr_distance);
        tv_vp_car_license = (TextView) view.findViewById(R.id.tv_vp_car_license);
        tv_vp_car_battery = (TextView) view.findViewById(R.id.tv_vp_car_battery);
        tv_vp_car_seats = (TextView) view.findViewById(R.id.tv_vp_car_seats);
        iv_vp_pic = (ImageView) view.findViewById(R.id.iv_vp_pic);
        tv_vp_price = (TextView) view.findViewById(R.id.tv_vp_price);
        tv_vp_start_price = (TextView) view.findViewById(R.id.tv_vp_start_price);
        tv_vp_outtime = (TextView) view.findViewById(R.id.tv_vp_outtime);
        tv_vp_car_license.setText(list.get(position).getLicenseTag());
        tv_vp_carr_distance.setText(SharedPreferencesUtils.getInstance(context).getStringValue("parkDistance") + "千米");
        tvVpCarBrand.setText(list.get(position).getBrand() + list.get(position).getModel());
        tv_vp_car_battery.setText(list.get(position).getMileage() + "公里");
        tv_vp_car_seats.setText(list.get(position).getSeat() + "座");
        tv_vp_outtime.setText("时长费：" + (list.get(position).getValuationRuleModel().getWeekDayPrice()) / 100.0 + "元/分钟");
        tv_vp_price.setText("里程费：" + (list.get(position).getValuationRuleModel().getMileagePrice()) / 100.0 + "元/公里");
        tv_vp_start_price.setText("起步价：" + (list.get(position).getValuationRuleModel().getStartingPrice()) / 100.0 + "元/公里");
        Glide.with(context).load(((MyApplication) context.getApplicationContext()).getImageUrl() + list.get(position).getPicture()).into(iv_vp_pic);
        tv_vp_park_name = (TextView) view.findViewById(R.id.tv_vp_park_name);
        if (list.get(position).getParkName() == null || list.get(position).getParkName().equals("")) {
            GeoCoderUtils geoCoderUtils = new GeoCoderUtils();
            geoCoderUtils.reverseGeoCode(new LatLng(list.get(position).getLatitude(), list.get(0).getLongitude()), tv_vp_park_name);
        } else {
            tv_vp_park_name.setText(list.get(position).getParkName());
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public void refreshData(List<ParkDetailBean.AttachmentBean.CarsBean> data) {
        list.clear();
        list.addAll(data);
        notifyDataSetChanged();
    }

    public ParkDetailBean.AttachmentBean.CarsBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
